package org.jeecg.modules.business.outbound.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.BeanUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.material.entity.BasicMaterial;
import org.jeecg.modules.basic.material.mapper.BasicMaterialMapper;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouse;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseArea;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseAreaService;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseService;
import org.jeecg.modules.basic.warehouseGroup.service.IWmsBasicWarehouseGroupUserService;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceiptMaterial;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterialExport;
import org.jeecg.modules.business.outbound.entity.dto.ListWmsOutReceiptDTO;
import org.jeecg.modules.business.outbound.entity.dto.WmsOutReceiptTaskDTO;
import org.jeecg.modules.business.outbound.entity.vo.*;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.outbound.vo.WmsOutReceiptTaskExport;
import org.jeecg.modules.business.outbound.vo.WmsOutReceiptTaskMaterialResponseVO;
import org.jeecg.modules.business.outbound.vo.WmsOutReceiptTaskPage;
import org.jeecg.modules.flow.callback.enums.OutboundStatusEnum;
import org.jeecg.modules.flow.callback.enums.TransferStatusEnum;
import org.jeecg.modules.flow.callback.outbound.util.CommonOutboundUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: 出库任务
 * @Author: jeecg-boot
 * @Date: 2023-08-09
 * @Version: V1.0
 */
@Api(tags = "出库任务")
@RestController
@RequestMapping("/business/wmsOutReceiptTask")
@Slf4j
public class WmsOutReceiptTaskController {
    @Autowired
    private IWmsOutReceiptTaskService wmsOutReceiptTaskService;
    @Autowired
    private IWmsOutReceiptTaskMaterialService wmsOutReceiptTaskMaterialService;
    @Resource
    private BasicMaterialMapper basicMaterialMapper;

    @Resource
    private IWmsBasicWarehouseService wmsBasicWarehouseService;

    @Resource
    private IWmsBasicWarehouseAreaService wmsBasicWarehouseAreaService;

    @Resource
    private IWmsOutReceiptService wmsOutReceiptService;

    @Resource
    private CommonOutboundUtil commonOutboundUtil;

    @Resource
    private IWmsBasicWarehouseGroupUserService wmsBasicWarehouseGroupUserService;

    /**
     * 出库任务下查
     *
     * @param wmsOutReceiptId
     * @return
     */
    @ApiOperation(value = "出库任务下查", notes = "出库任务下查")
    @GetMapping(value = "/listWmsOutReceipt")
    public Result<List<ListWmsOutReceiptDTO>> listWmsOutReceipt(@RequestParam("wmsOutReceiptId") String wmsOutReceiptId) {
        List<ListWmsOutReceiptDTO> list = new ArrayList<>();
        ListWmsOutReceiptDTO listWmsOutReceiptDTO = wmsOutReceiptTaskService.listWmsOutReceipt(wmsOutReceiptId);
        list.add(listWmsOutReceiptDTO);
        return Result.OK(list);
    }

    /**
     * 分页列表查询
     *
     * @param wmsOutReceiptTask
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "出库任务-分页列表查询")
    @ApiOperation(value = "出库任务-分页列表查询", notes = "出库任务-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WmsOutReceiptTaskPageVO>> queryPageList(WmsOutReceiptTaskQueryVO wmsOutReceiptTask, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        IPage<WmsOutReceiptTask> page = new Page<>(pageNo, pageSize);
        IPage<WmsOutReceiptTaskPageVO> pageList = wmsOutReceiptTaskService.listByPage(page, wmsOutReceiptTask);
        List<WmsOutReceiptTaskPageVO> records = pageList.getRecords();
        List<String> taskIds = records.stream().map(WmsOutReceiptTask::getId).collect(Collectors.toList());
        List<WmsOutReceiptTaskMaterial> list = wmsOutReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
                .in(CollectionUtil.isNotEmpty(taskIds), WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId, taskIds));
        for(WmsOutReceiptTaskPageVO pageVO:records){
            List<WmsOutReceiptTaskMaterial> taskMaterialList = list.stream().filter(x -> x.getWmsOutReceiptTaskId().equals(pageVO.getId())).collect(Collectors.toList());
            if(!CollectionUtil.isEmpty(taskMaterialList)){
                pageVO.setHasChild(1);
                //设置车牌号
                Set<String> carNumberSet = taskMaterialList.stream().filter(o->!StrUtil.isEmpty(o.getCarNumber())).map(WmsOutReceiptTaskMaterial::getCarNumber).collect(Collectors.toSet());
                pageVO.setCarNumberStr(StrUtil.join(",",carNumberSet));
                //设置仓库名称
                Set<String> warehouseIdSet = taskMaterialList.stream().filter(o->!StrUtil.isEmpty(o.getWmsBasicWarehouseId())).map(WmsOutReceiptTaskMaterial::getWmsBasicWarehouseId).collect(Collectors.toSet());
                List<WmsBasicWarehouse> warehouseList=new ArrayList<>();
                if(!CollectionUtil.isEmpty(warehouseIdSet)){
                    warehouseList = wmsBasicWarehouseService.list(new LambdaQueryWrapper<WmsBasicWarehouse>()
                            .in(CollectionUtil.isNotEmpty(warehouseIdSet), WmsBasicWarehouse::getId, warehouseIdSet)
                    );
                }
                Set<String> warehouseCodeSet = warehouseList.stream().map(WmsBasicWarehouse::getName).collect(Collectors.toSet());
                pageVO.setWarehouseCodeStr(StrUtil.join(",",warehouseCodeSet));
                //设置出库订单号
                pageVO.setOutboundOrderCode(pageVO.getOriginalSystemNumber());
                //设置客户代码 数据中已经有了
            }else{
                pageVO.setHasChild(0);
            }
            //设置任务的过账标识
            if(TransferStatusEnum.TRANSFER_FAIL.getCode().equals(pageVO.getTransferSapStatus())){
                pageVO.setTransferButton(true);
            }else{
                pageVO.setTransferButton(false);
            }
        }
        pageList.setRecords(records);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param wmsOutReceiptTaskPage
     * @return
     */
    @AutoLog(value = "出库任务-添加")
    @ApiOperation(value = "出库任务-添加", notes = "出库任务-添加")
    @RequiresPermissions("business:wms_out_receipt_task:add")
    @PostMapping(value = "/add")
    public Result add(@RequestBody WmsOutReceiptTaskPage wmsOutReceiptTaskPage) {
        Result<String> r = new Result();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        WmsOutReceiptTask wmsOutReceiptTask = new WmsOutReceiptTask();
        BeanUtils.copyProperties(wmsOutReceiptTaskPage, wmsOutReceiptTask);
        String s = wmsOutReceiptTaskService.saveMain(wmsOutReceiptTask, wmsOutReceiptTaskPage.getWmsOutReceiptTaskMaterialList());
        r.setResult(s);
        return Result.OK(r);
    }

    /**
     * 编辑
     *
     * @param wmsOutReceiptTaskPage
     * @return
     */
    @AutoLog(value = "出库任务-编辑")
    @ApiOperation(value = "出库任务-编辑", notes = "出库任务-编辑")
    @RequiresPermissions("business:wms_out_receipt_task:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody WmsOutReceiptTaskPage wmsOutReceiptTaskPage) {
        WmsOutReceiptTask wmsOutReceiptTask = new WmsOutReceiptTask();
        BeanUtils.copyProperties(wmsOutReceiptTaskPage, wmsOutReceiptTask);
        WmsOutReceiptTask wmsOutReceiptTaskEntity = wmsOutReceiptTaskService.getById(wmsOutReceiptTask.getId());
        if (wmsOutReceiptTaskEntity == null) {
            return Result.error("未找到对应数据");
        }
        wmsOutReceiptTaskService.updateMain(wmsOutReceiptTask, wmsOutReceiptTaskPage.getWmsOutReceiptTaskMaterialList());
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "出库任务-通过id删除")
    @ApiOperation(value = "出库任务-通过id删除", notes = "出库任务-通过id删除")
    @RequiresPermissions("business:wms_out_receipt_task:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        wmsOutReceiptTaskService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "出库任务-批量删除")
    @ApiOperation(value = "出库任务-批量删除", notes = "出库任务-批量删除")
    @RequiresPermissions("business:wms_out_receipt_task:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wmsOutReceiptTaskService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "出库任务-通过id查询")
    @ApiOperation(value = "出库任务-通过id查询", notes = "出库任务-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmsOutReceiptTask> queryById(@RequestParam(name = "id", required = true) String id) {
        WmsOutReceiptTask wmsOutReceiptTask = wmsOutReceiptTaskService.getById(id);
        if (wmsOutReceiptTask == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsOutReceiptTask);

    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "出库任务-物料通过主表ID查询")
    @ApiOperation(value = "出库任务-物料主表ID查询", notes = "出库任务-物料-通主表ID查询")
    @GetMapping(value = "/queryWmsOutReceiptTaskMaterialByMainId")
    public Result<List<WmsOutReceiptTaskMaterialResponseVO>> queryWmsOutReceiptTaskMaterialListByMainId(@RequestParam(name = "id", required = true) String id,
                                                                                                        @RequestParam(name = "name", required = false) String name,
                                                                                                        @RequestParam(name = "unCompleted", required = false) Boolean unCompleted,
                                                                                                        @RequestParam(name = "pdaFilterFlag", required = false) Boolean pdaFilterFlag
                                                                                                        ) {
        List<WmsOutReceiptTaskMaterialResponseVO> list=wmsOutReceiptTaskService.queryWmsOutReceiptTaskMaterialListByMainId(id,name,unCompleted,pdaFilterFlag);
        return Result.OK(list);
    }

    /**
     * 出库任务-物料主表ID查询-pda用带即时库存
     *
     * @param pdaTaskMaterialReqVO
     * @return
     */
    @ApiOperation(value = "出库任务-物料主表ID查询-pda用带即时库存", notes = "出库任务-物料主表ID查询-pda用带即时库存")
    @PostMapping(value = "/pda/queryTaskMaterialByTaskId")
    public Result<List<WmsOutReceiptTaskMaterialResponseVO>> pdaQueryTaskMaterialByTaskId(@RequestBody PdaTaskMaterialReqVO pdaTaskMaterialReqVO) {

        List<WmsOutReceiptTaskMaterialResponseVO> list=wmsOutReceiptTaskService.pdaQueryTaskMaterialByTaskId(pdaTaskMaterialReqVO);
        return Result.ok(list);
    }

    @ApiOperation(value = "出库任务-物料主表ID分页查询", notes = "出库任务-物料主表ID分页查询")
    @GetMapping(value = "/queryPageTaskMaterialByTaskId")
    public Result<IPage<WmsOutReceiptTaskMaterialResponseVO>> queryPageTaskMaterialByTaskId(@RequestParam(name = "id", required = true) String id,
                                                                                                        @RequestParam(name = "name", required = false) String name,
                                                                                                        @RequestParam(name = "deviceType", required = false) String deviceType,
                                                                                                        @RequestParam(name = "unCompleted", required = false) Integer unCompleted,
                                                                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        IPage<WmsOutReceiptTaskMaterial> page = new Page<>(pageNo, pageSize);
        IPage<WmsOutReceiptTaskMaterialResponseVO> pageList = wmsOutReceiptTaskMaterialService.queryPageTaskMaterialByTaskId(page,id,name,deviceType,unCompleted);
        return Result.OK(pageList);
    }


    /**
     * 导出excel
     *
     * @param request
     * @param wmsOutReceiptTaskQueryVO
     */
    @ApiOperation(value = "导出excel", notes = "导出excel")
    @RequiresPermissions("business:wms_out_receipt_task:exportXls")
    @PostMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsOutReceiptTaskQueryVO wmsOutReceiptTaskQueryVO) {
        WmsOutReceiptTaskDTO wmsOutReceiptTaskDTO = new WmsOutReceiptTaskDTO();

        WmsOutReceiptTask wmsOutReceiptTask = new WmsOutReceiptTask();
        BeanUtils.copyProperties(wmsOutReceiptTaskQueryVO, wmsOutReceiptTask);
        // Step.1 组装查询条件查询数据
        QueryWrapper<WmsOutReceiptTask> queryWrapper = QueryGenerator.initQueryWrapper(wmsOutReceiptTask, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        //配置选中数据查询条件
        String selections = request.getParameter("selections");
        List<WmsOutReceiptTask> wmsOutReceiptTaskList = null;
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            queryWrapper.in("id", selectionList);
            wmsOutReceiptTaskDTO.setWmsOutReceiptTaskIdList(selectionList);
            wmsOutReceiptTaskList = wmsOutReceiptTaskService.list(queryWrapper);
        } else {
            BeanUtils.copyProperties(wmsOutReceiptTaskQueryVO, wmsOutReceiptTaskDTO);
            if (ObjectUtil.isNotEmpty(wmsOutReceiptTaskDTO.getDocumentType())) {
                List<String> documentTypeList = Arrays.asList(StringUtils.split(wmsOutReceiptTaskDTO
                        .getDocumentType(), ","));
                wmsOutReceiptTaskDTO.setDocumentTypeList(documentTypeList);
            }
            if (ObjectUtil.isNotEmpty(wmsOutReceiptTaskDTO.getOutWarehouseId())) {
                List<String> outWarehouseIds = Arrays.asList(StringUtils.split(wmsOutReceiptTaskDTO
                        .getOutWarehouseId(), ","));
                wmsOutReceiptTaskDTO.setOutWarehouseIds(outWarehouseIds);
            }
            if (!StrUtil.isEmpty(wmsOutReceiptTaskDTO.getDocumentStatus())) {
                List<String> documentStatusList = Arrays.asList(StrUtil.split(wmsOutReceiptTaskDTO
                        .getDocumentStatus(), ","));
                wmsOutReceiptTaskDTO.setDocumentStatusList(documentStatusList);
            }
            //根据当前用户权限过滤
            List<String> loginUserWarehouseIdList = wmsBasicWarehouseGroupUserService.getLoginUserWarehouseIdList();
            wmsOutReceiptTaskDTO.setLoginUserWarehouseIdList(loginUserWarehouseIdList);
            IPage<WmsOutReceiptTask> page = new Page<>(1, Long.MAX_VALUE);
            IPage<WmsOutReceiptTaskPageVO> pageList = wmsOutReceiptTaskService.listByPage(page, wmsOutReceiptTaskDTO);
            List<WmsOutReceiptTaskPageVO> records = pageList.getRecords();
            wmsOutReceiptTaskList = records.stream().map(vo -> (WmsOutReceiptTask) vo).collect(Collectors.toList());
        }

        //Step.2 获取导出数据


        // Step.3 组装pageList
        List<WmsOutReceiptTaskExport> pageList = new ArrayList<>();
//        if(!CollectionUtil.isEmpty(wmsOutReceiptTaskList)){
//            List<String> taskIdList = wmsOutReceiptTaskList.stream().map(WmsOutReceiptTask::getId).collect(Collectors.toList());
//            List<WmsOutReceiptTaskMaterial> wmsOutReceiptTaskMaterialList=wmsOutReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
//                    .in(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId,taskIdList)
//            );
//            for (WmsOutReceiptTask main : wmsOutReceiptTaskList) {
//                WmsOutReceiptTaskPage vo = new WmsOutReceiptTaskPage();
//                BeanUtils.copyProperties(main, vo);
//                if(!CollectionUtil.isEmpty(wmsOutReceiptTaskMaterialList)){
//                    List<WmsOutReceiptTaskMaterial> collect = wmsOutReceiptTaskMaterialList.stream().filter(taskMaterial -> taskMaterial.getWmsOutReceiptTaskId().equals(main.getId())).collect(Collectors.toList());
//                    vo.setWmsOutReceiptTaskMaterialList(collect);
//                }
//                pageList.add(vo);
//            }
//        }

         /*优化代码 by DuSongYao 2024.01.09  Start*/
        if (CollectionUtil.isNotEmpty(wmsOutReceiptTaskList)){
            List<WmsOutReceiptTaskMaterialExport> wmsOutReceiptTaskMaterialList = wmsOutReceiptTaskMaterialService
                    .listOutMaterialExportByTask(wmsOutReceiptTaskDTO);
            Map<String, List<WmsOutReceiptTaskMaterialExport>> wmsOutReceiptTaskIdToMaterialListMap = new HashMap<>(0);
            if (CollectionUtil.isNotEmpty(wmsOutReceiptTaskMaterialList)){
                // 使用Java 8的Stream API和Collectors.groupingBy方法来分组
                wmsOutReceiptTaskIdToMaterialListMap =  wmsOutReceiptTaskMaterialList.stream()
                        .collect(Collectors.groupingBy(WmsOutReceiptTaskMaterialExport::getWmsOutReceiptTaskId));
            }
            // 遍历 wmsOutReceiptTaskList 创建 pageList
            for (WmsOutReceiptTask task : wmsOutReceiptTaskList) {
                WmsOutReceiptTaskExport ele = new WmsOutReceiptTaskExport();
                BeanUtils.copyProperties(task, ele);
                // 从映射中获取物料列表，如果不存在则使用空列表
                List<WmsOutReceiptTaskMaterialExport> materials = wmsOutReceiptTaskIdToMaterialListMap
                        .getOrDefault(task.getId(), Collections.emptyList());
                ele.setWmsOutReceiptTaskMaterialExportList(materials);
                pageList.add(ele);
            }
        }
        /*优化代码 by DuSongYao 2024.01.09  End*/

        // Step.4 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "出库任务列表");
        mv.addObject(NormalExcelConstants.CLASS, WmsOutReceiptTaskExport.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("出库任务数据", "导出人:" + sysUser.getRealname(), "出库任务"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
    @RequiresPermissions("business:wms_out_receipt_task:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<WmsOutReceiptTaskPage> list = ExcelImportUtil.importExcel(file.getInputStream(), WmsOutReceiptTaskPage.class, params);
                for (WmsOutReceiptTaskPage page : list) {
                    WmsOutReceiptTask po = new WmsOutReceiptTask();
                    BeanUtils.copyProperties(page, po);
                    wmsOutReceiptTaskService.saveMain(po, page.getWmsOutReceiptTaskMaterialList());
                }
                return Result.OK("文件导入成功！数据行数:" + list.size());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.OK("文件导入失败！");
    }

    /**
     * 获取物料即时库存位置信息
     * @return
     */
    @ApiOperation(value = "获取物料即时库存位置信息", notes = "获取物料即时库存位置信息")
    @GetMapping(value = "/getMaterialRepertoryInfo")
    public Result<RepertoryLocationVO> getMaterialRepertoryInfo(@RequestParam(value = "taskMaterialId",required = false)String taskMaterialId){
        try {
            RepertorySimpleVO simpleVO=wmsOutReceiptTaskService.getMaterialRepertoryInfoByOutId(taskMaterialId);
            RepertoryLocationVO repertoryLocationVO = cn.hutool.core.bean.BeanUtil.toBean(simpleVO, RepertoryLocationVO.class);
            return Result.ok(repertoryLocationVO);
        }catch (JeecgBootException e){
            return Result.error(-100,e.getMessage());
        }
    }

    /**
     * 获取物料即时库存位置信息--前端调用
     * @return
     */
    @ApiOperation(value = "获取物料即时库存位置信息", notes = "获取物料即时库存位置信息")
    @GetMapping(value = "/getMaterialRepertory")
    public Result<RepertoryLocationVO> getMaterialRepertory(@RequestParam(value = "taskMaterialId")String taskMaterialId){
        return wmsOutReceiptTaskService.getMaterialRepertory(taskMaterialId);
    }

    /**
     * 出库任务或者出库单过账
     * @return
     */
    @ApiOperation(value = "出库任务或者出库单-过账", notes = "出库任务或者出库单-过账")
    @PostMapping("/outTaskAndReceiptTransfer")
    public Result<Void> outTaskAndReceiptTransfer(@RequestParam(value = "taskId",required = false)String taskId,@RequestParam(value="receiptId",required = false)String receiptId){
        List<String> errorList=wmsOutReceiptTaskService.outTaskAndReceiptTransfer(taskId,receiptId);
        if(CollectionUtil.isEmpty(errorList)){
            return Result.ok();
        }else {
            return Result.error(StrUtil.join(",",errorList));
        }
    }

    /**
     * 检查物料是否存在在执行中的入库任务中
     * @return
     */
    @ApiOperation(value = "检查物料是否存在在执行中的入库任务中", notes = "检查物料是否存在在执行中的入库任务中")
    @PostMapping("/checkMaterialExistProcessInTask")
    public Result<NoticeInTaskVO> checkMaterialExistProcessInTask(@RequestBody List<CheckMaterialVO> checkMaterialVOList){
        NoticeInTaskVO noticeInTaskVO=wmsOutReceiptTaskService.checkMaterialExistProcessInTask(checkMaterialVOList);
        return Result.ok(noticeInTaskVO);
    }

    /**
     * pda 检查物料是否存在在执行中的入库任务中
     * @param pdaCheckMaterialVO
     * @return
     */
    @ApiOperation(value = "pda检查物料是否存在在执行中的入库任务中", notes = "pda检查物料是否存在在执行中的入库任务中")
    @PostMapping("pda/checkMaterialExistProcessInTask")
    public Result<NoticeInTaskVO> pdaCheckMaterialExistProcessInTask(@RequestBody PdaCheckMaterialVO pdaCheckMaterialVO){

        List<CheckMaterialVO> materialVOList = pdaCheckMaterialVO.getMaterialVOList();
        NoticeInTaskVO noticeInTaskVO=wmsOutReceiptTaskService.checkMaterialExistProcessInTask(materialVOList);
        return Result.ok(noticeInTaskVO);
    }

    /**
     * pda视觉识别鞍座
     *
     * @author hansen
     * @since 2024/1/26 13:12
     * @param id, saddle
     * @return org.jeecg.common.api.vo.Result<java.lang.Void>
     */
    @ApiOperation(value = "pda视觉识别鞍座", notes = "pda视觉识别鞍座")
    @PostMapping("/pad/signalTrigger")
    public Result<Result<String>> pdaSignalTrigger(@RequestParam("id") String id,@RequestParam("saddle") String saddle){
        return wmsOutReceiptTaskService.pdaSignalTrigger(id, saddle);
    }

}
